//
//  NavigateViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "NavigateViewController.h"



@interface NavigateViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *dataArr;//接收的数组
@property(nonatomic, retain)ShipuModel *model;
@property(nonatomic,retain)UIView *hudView; //侧栏滑动
@property(nonatomic ,assign)BOOL isClick;
@property(nonatomic, retain) UIView *backView; //设置灰色背景
@property(nonatomic ,retain) UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *classArr;//分区的数组
@property(nonatomic ,retain)NSMutableArray *allArr;//allTextArr对应的每个元素数组
@property(nonatomic,assign)NSInteger index; //中间变量
@property(nonatomic ,retain)NSMutableArray *tArr;  //中间转换数组
@property(nonatomic,retain)NSMutableArray *allTextArr; //文字转换成数组存放的数组
@end

@implementation NavigateViewController

- (void)dealloc
{
    [_classArr release];
    [_collectionView release];
    [_dataArr release];
    [_model release];
    [_hudView release];
    [_backView release];
    [_allTextArr release];
    [_allArr release];
    [_tArr release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    
    self.navigationController.navigationBar.translucent = NO;
   //判断时哪一个导航点进来的 实现不同的界面
    if ([self.s_Model.text isEqualToString:@"新手入门"]) {
    self.navigationItem.title = self.s_Model.text;
    UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height )];
    /**<  取消回弹效果 */
    wv.scrollView.bounces = NO;
    // 加载网址
    NSString *url =self.s_Model.link;
    
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    [self.view addSubview:wv];
    [wv release];
}
    if ([self.s_Model.text isEqualToString:@"食材搭配"]) {
        self.navigationItem.title = self.s_Model.text;
        
         [self dataHandler];
        [self creatCollectionView];
        [self creatView];
      
       
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
}
-(void)leftAction:(UIBarButtonItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
}



//数据请求
-(void)dataHandler
{
    NSString *str = @"http://api.izhangchu.com/?methodName=MaterialSubtype&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr = responseObject[@"data"][@"data"];
       
        self.classArr= [ ShipuModel modelHanderWithArray:arr];
        [_tableView reloadData];
        self.dataArr = [ NSMutableArray array];
        self.allTextArr = [ NSMutableArray array];
        // NSArray *iArr = arr[_index][@"data"];
        
        for (NSDictionary *dic1 in arr) {
            //获取到大的数组
            NSArray *iArr = dic1[@"data"];
            self.model = [[ShipuModel alloc] init];
            _model.imageArr = [ NSMutableArray array];
            _model.textArr = [ NSMutableArray array];
            //遍历取到每个data里的字典中的image值
            for (NSDictionary *dic2 in iArr) {
                [_model.imageArr addObject:dic2[@"image"]];
                [_model.textArr addObject:dic2[@"text"]];
            }
            //图片数组
            [self.dataArr addObject:_model.imageArr];
            //文字数组
            [self.allTextArr addObject:_model.textArr];
            
            
        }
             [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}
//创建弹动视图
-(void)creatView
{
    
    
    UIButton *button =[ UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor =[ UIColor grayColor];
    button.frame = CGRectMake(WIDTH*(335 + 20),HEIGHT * 150,WIDTH * 20,HEIGHT * 40);
//    [button setBackgroundImage:[UIImage imageNamed:@"more@2x"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectionView addSubview:button];
    
}
-(void)btn:(UIButton *)b
{    if(!_isClick){
    
   
    
    self.hudView =[[ UIView alloc] initWithFrame:CGRectMake(175 * WIDTH,HEIGHT * 0,WIDTH * 200, 667 *HEIGHT)];
    [self creatTableView];
    b.frame = CGRectMake((135+20) *WIDTH,HEIGHT* 150,WIDTH * 20,HEIGHT *40);
    
    _backView.alpha = 0.5;
    _hudView.backgroundColor= [ UIColor whiteColor];
     b.backgroundColor = [ UIColor whiteColor];
    [self.view addSubview:_hudView];
    [_hudView release];
   
}
else
{
    self.hudView.frame= CGRectMake(375 *WIDTH,HEIGHT* 0,WIDTH* 200,HEIGHT * 667);
    b.frame = CGRectMake((335+20) *WIDTH,HEIGHT * 150,WIDTH * 20, 40*HEIGHT);
    b.backgroundColor = [ UIColor grayColor];
    _backView.alpha = 0;
    
}
    _isClick = !_isClick;
    
    
}
#pragma mark -collectionview
-(void)creatCollectionView
{
    UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    //cell大小
    layout.itemSize = CGSizeMake((WIDTH*375 - 20 *WIDTH) /4 ,HEIGHT*60);
    //行列间距
    //行
    layout.minimumLineSpacing = WIDTH*40;
    //列
    layout.minimumInteritemSpacing = 0;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(WIDTH * 10,HEIGHT * 10,WIDTH * 100,HEIGHT*10);
    _collectionView =[[ UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375*WIDTH, 667*HEIGHT)  collectionViewLayout:layout];
    _collectionView .delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView setContentOffset:CGPointMake(WIDTH*375 *2, 0)];
    [self.view addSubview:_collectionView];
    //[_collectionView release];
    _collectionView.pagingEnabled = NO;
    [_collectionView registerClass:[MaterialCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    self.backView = [[ UIView alloc] initWithFrame:self.collectionView.frame];
    _backView.backgroundColor = [UIColor blackColor];
    [_collectionView addSubview:_backView];
    _backView.alpha = 0;
    [_backView release];
    
    }
#pragma mark -必须实现的方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{ //取到图片数组中对应的数组
        _allArr = _dataArr[_index];

        return  _allArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MaterialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
     _tArr = _allTextArr[_index];
      cell.name.text = _tArr[indexPath.item];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:_allArr[ indexPath.item]]];
      
    return cell;
    
    
}
#pragma mark - 弹动view的tableview
-(void)creatTableView
{
    self.tableView = [[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, WIDTH *667) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [ UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView .contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_hudView addSubview:_tableView];
    [_tableView release];
}

#pragma mark -tableview必须实现的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _classArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
     ;
    cell.textLabel.text= [_classArr[ indexPath.row] text];
    
    return cell;
}
//点击方法  记录点击数组的下标
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      self.index = indexPath.row;
    [_collectionView reloadData];
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
